# ------------------------------------------------------------------------------
# Uses logistic regression to predict testing from K vars selected in lasso
# Author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bsub -q short -R "rusage[mem=100]" bash 02_fit_unpenalized_along_path.sh
# ------------------------------------------------------------------------------

# Seeding ----------------------------------------------------------------------
set.seed(1)

# Libraries --------------------------------------------------------------------
library(here)
library(yaml)
library(glue)
library(data.table)
library(Matrix)
library(tidyverse)
library(glmnet)
library(fastglm)

temp <- here(
  "code", "06_physician_boundedness", "01_behavioral_lasso", "temp"
)
u <- modules::use(here("lib", "util.R"))

# Load Data --------------------------------------------------------------------
message("Loading data...")
paths <- read_yaml(here("lib", "filepaths.yml"))
split <- "random"
overnight_lab <- ""
x <- readRDS(glue(paths$features$train_tested))
cohort_fp <- file.path(paths$modeling$dir, "cohorts", "random")
ids <- readRDS(file.path(cohort_fp, "train_cohort_tested.rds"))

model <- readRDS(file.path(temp, glue("lasso__stent_or_cabg_010_day.rds")))

keep <- which(!ids$exclude)

x <- x[keep, ] %>%
  as.matrix %>%
  as.data.frame %>%
  mutate(Constant = 1)
ids <- ids[keep, ]

# Get Selected Variables -------------------------------------------------------
# Get coefficients except intercept
coef_mtx <- model$beta
selected_cols <- apply(coef_mtx, 2, function(x) which(x != 0))
selected_cols <- unique(selected_cols)

# Drop duplicates
which_knots <- between(map_int(selected_cols, length), 1, 200)
selected_cols_sub <- selected_cols[which_knots]

# Fit Regressions --------------------------------------------------------------
yield <- ids$stent_or_cabg_010_day

glms <- tibble(
  which_cols = selected_cols_sub,
  fit = map(
    selected_cols_sub,
    ~ glm(
      yield ~ .,
      data = cbind(yield, x[,c(.x, ncol(x))]),
      family = binomial()
    )
  )
)

# Save -------------------------------------------------------------------------
write_rds(glms, file.path(temp, "unpenalized_glm__stent_or_cabg_010_day.rds"))

# ------------------------------------------------------------------------------
message("Done.")
